/**
 * 
 * @author:huangyuan
 * @Email:huangyuan3h@gmail.com description:here is the api for the ajax,it is
 *                              only used in this project. it base on jquery
 * 
 * 
 */

window.rowCount = 1;

// soap table initial
function initialSoap() {
	$("#addRowButton").click(
			function() {

				window.rowCount = window.rowCount + 1;

				str = "<tr class='Parameter'><td>name" + rowCount + ":</td><td><input type='text' id='name" + rowCount
						+ "' class='name'></td><td>argue" + rowCount + ":</td>" + "<td><input type='text' id='argue"
						+ rowCount + "' class='value'><a href='#' class='remove'>remove</a></td></tr>";

				$("#soapParameter").append(str);

				$(".remove").click(function() {

					$(this).parent().parent().remove();

				});

			});

	$("#SoapPart").hide();// default hiden

}

// some function belong to rest
function initialRest() {

}

// control table value
function tableControl() {

	$("#RestType").click(function() {
		$("#RestPart").show();
		$("#SoapPart").hide();
		$("#RestType").attr("checked", "checked");
		$("#SoapType").attr("checked", false);
	});

	$("#SoapType").click(function() {

		$("#SoapPart").show();
		$("#RestPart").hide();
		$("#SoapType").attr("checked", "checked");
		$("#RestType").attr("checked", false);
	});

	$("input[type='radio'][name='sendType']").click(function() {
		$("#GetType").attr("checked", false);
		$("#PostType").attr("checked", false);
		$("#putType").attr("checked", false);
		$("#DeleteType").attr("checked", false);
		$(this).attr("checked", "checked");

	});

}

// do ajax function
function ajax() {
	$("#submit").click(function() {

		if ("Rest" == $("input[type='radio'][name='potocalType'][checked='checked']").val()) {

			restAjax();

		} else {
			soapAjax();
		}

	});

}

function soapAjax() {
	debugger
	var type = $("input[type='radio'][name='sendType'][checked='checked']").val();
	var url = $("#url").val();
	var data = new Array();
	$('.Parameter').each(function() {
		data[$(this).children().children(".name").val()] = $(this).children().children(".value").val();

	});

	if (data.length == 0) {
		data = null;
	}
	var MyData = new Object();
	MyData = JSON.parse(data);

	$.ajax({
		type : type,
		data : MyData,
		url : url,
		contentType : "application/json; charset=utf-8",
		dataType : "html",
		success : function(data) {
			alert(data);
		},
		error : function() {
			alert("error");
		}

	});
}

function restAjax() {

	var url = $("#url").val();
	var type = $("input[type='radio'][name='sendType'][checked='checked']").val();
	var data = $("#restData").val();
	data.Trim();
	if (data.Trim() == "" || data.length == 0) {

		data = null;
	}

	var MyData = new Object();
	MyData = JSON.parse(data);
	/*
	 * var request = false; try { request = new XMLHttpRequest(); } catch
	 * (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
	 * catch (othermicrosoft) { try { request = new
	 * ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; } } }
	 * if (!request) alert("Error initializing XMLHttpRequest!");
	 * request.onreadystatechange = updatePage; request.open("POST", url, true);
	 * request.setRequestHeader("Content-type","application/json");
	 * request.send(data); function updatePage() { if (request.readyState == 4) {
	 * if (request.status == 200) { debugger; var response =
	 * request.responseText.split("|");
	 *  } else alert("status is " + request.status); } }
	 */
	debugger
	$.ajax({
		type : type,
		data : JSON.stringify(MyData),
		url : url,
		contentType : "application/json; charset=utf-8",
		dataType : "html",
		headers : {
			"accept" : "application/rdf+xml"// accept header
		},
		success : function(data) {
			alert("successful,return data is" + data);
		},
		error : function() {
			alert("error");
		}
	});

}

$(function() {

	$("input[type='button']").button();
	$("#SendTypeRadio").buttonset();
	$("#potocalType").buttonset();

	initialSoap();
	initialRest();
	ajax();
	tableControl();
});
